package com.twu.biblioteca;

import com.twu.biblioteca.exception.ItemUnavailableException;

import java.util.ArrayList;
import java.util.List;

public class ItemStatus {

    private final Item item;
    private int quantity;
    private List<User> users;

    public ItemStatus(Item item, int quantity) {
        this.item = item;
        this.quantity = quantity;
        this.users = new ArrayList<>();
    }

    public void checkout(User user) throws ItemUnavailableException {
        if (isAvailableToCurrentUser()) {
            users.add(user);
            quantity--;
        } else {
            throw new ItemUnavailableException();
        }
    }

    public boolean isAvailableToCurrentUser() {
        boolean available = false;
        if (quantity > 0) {
            available = true;
        }
        return available;
    }

    public Item getItem() {
        return this.item;
    }

    public void returnItem(User user) throws ItemUnavailableException {
        if (users.contains(user)) {
            this.quantity++;
            users.remove(user);
        } else {
            throw new ItemUnavailableException();
        }

    }

    public int getQuantity() {
        return quantity;
    }

    public boolean is(String itemName) {
        return item.getName().equals(itemName);
    }
}
